﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;

namespace MINERP.BLL.InfoConfig
{
    public class ShopTechUserBLL
    {

        /// <summary>
        /// 获得shopTechUser列表
        /// </summary>
        /// <returns></returns>
        public List<ShopTechUserInfo> GetShopTechUserInfoList()
        {
            List<ShopTechUserInfo> list = new List<ShopTechUserInfo>();
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                List<Shop_Tech_User> shopTechUserList = entities.Shop_Tech_User.ToList();
                foreach (var item in shopTechUserList)
                {
                    if(!item.ShopReference.IsLoaded)
                    {
                        item.ShopReference.Load();
                    }
                    if(!item.tb_TechReference.IsLoaded)
                    {
                        item.tb_TechReference.Load();
                    }
                    if(!item.usersReference.IsLoaded)
                    {
                        item.usersReference.Load();
                    }
                    ShopTechUserInfo shopTechUserInfo = new ShopTechUserInfo();
                    shopTechUserInfo.ShopName = item.Shop.shopName;
                    shopTechUserInfo.TechName = item.tb_Tech.TechName;
                    shopTechUserInfo.UserName = item.users.UserName;
                    list.Add(shopTechUserInfo);

                }
            }
            return list;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="shopName"></param>
        /// <param name="techName"></param>
        /// <param name="userName"></param>
        public void Add(string shopName, string techName, string userName)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                try
                {
                    Shop shop = entities.Shop.FirstOrDefault(s => s.shopName == shopName);
                    tb_Tech tech = entities.tb_Tech.FirstOrDefault(t => t.TechName == techName);
                    users user = entities.users.FirstOrDefault(u => u.UserName == userName);
                    Shop_Tech_User stu = new Shop_Tech_User();
                    stu.Shop = shop;
                    stu.tb_Tech = tech;
                    stu.users = user;
                    entities.AddToShop_Tech_User(stu);
                    entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="stuInfo"></param>
        /// <param name="techName"></param>
        /// <param name="userName"></param>
        public void Update(ShopTechUserInfo stuInfo,string techName,string userName)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                try
                {
                    Shop_Tech_User stu = entities.Shop_Tech_User.FirstOrDefault(s => s.Shop.shopName == stuInfo.ShopName && s.tb_Tech.TechName == stuInfo.TechName && s.users.UserName == stuInfo.UserName);
                    stu.tb_Tech = entities.tb_Tech.FirstOrDefault(t => t.TechName == techName);
                    stu.users = entities.users.FirstOrDefault(u => u.UserName == userName);
                    entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        public void Delete(ShopTechUserInfo stuInfo)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                try
                {
                    Shop_Tech_User stu = entities.Shop_Tech_User.FirstOrDefault(s => s.Shop.shopName == stuInfo.ShopName && s.tb_Tech.TechName == stuInfo.TechName && s.users.UserName == stuInfo.UserName);
                    entities.DeleteObject(stu);
                    entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        /// <summary>
        /// 判断同一商户的默认节点是否存在
        /// </summary>
        /// <param name="shopName"></param>
        /// <param name="techName"></param>
        /// <returns></returns>
        public bool IsExistSameShopTech(string shopName,string techName,string userName)
        {
            bool flag = false;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                Shop_Tech_User stu = entities.Shop_Tech_User.FirstOrDefault(s => s.Shop.shopName == shopName && s.tb_Tech.TechName == techName&&s.users.UserName ==userName);
                if (stu != null)
                {
                    flag = true;
                }
            }
            return flag;

        }



    }
}
